<?php

/**
 * Subclass for representing a row from the 'account_customer' table.
 *
 * 
 *
 * @package lib.model
 */ 
class AccountCustomer extends BaseAccountCustomer
{

  function AccountCustomer(){
    $this->setContactIden('');
    $this->setCurrencyCode('');
    $this->setDefaultTerms('');
    $this->setStatus('GOOD');
    $this->setRemarks('');
    $this->setCustomerIndustry('');
    $this->setTinNumber('');
    $this->setCreditLimit(0);
    $this->setVatExcemptType('NONE');
    $this->setDefaultOwnerIden('');
  }
  
  function getTotalReceivable(){
    $connection = Propel::getConnection();
    $query = 
      'SELECT sum('.LogTransactionPeer::DEBIT.'-'.LogTransactionPeer::CREDIT.') AS TOTAL '.
      'FROM '.LogTransactionPeer::TABLE_NAME.' '.
      'join '.DocumentPeer::TABLE_NAME.' '.
      'on '.DocumentPeer::DOCUMENT_IDEN.'='.LogTransactionPeer::DOCUMENT_IDEN.' '.
      'join '.InvoicePeer::TABLE_NAME.' '.
      'on '.DocumentPeer::DOCUMENT_IDEN.'='.InvoicePeer::INVOICE_IDEN.' '.
      'join '.ContactPeer::TABLE_NAME.' '.
      'on '.ContactPeer::IDEN.'='.InvoicePeer::CUSTOMER_IDEN.' ';
    $query .= 'where '.ContactPeer::IDEN.'=\''.$this->getContactIden().'\' ';
    $query .= 'having TOTAL > 0 ';
    $statement = $connection->prepare($query);
    $statement->execute();
    $receivables = $statement->fetchAll(PDO::FETCH_ASSOC);
    if($receivables)
      return $receivables[0]['TOTAL'];
    else
      return 0;
  }
  
  function getTotalCreditAmount(){
    
  }
  
  function getSupplierName(){
    return $this->getAccountOwner()->getContact()->getListName();
  }
  
}
